//
//  BCMyCollectionVC.m
//  BangcaiSale
//
//  Created by lsy on 16/7/7.
//  Copyright © 2016年 Bangcai. All rights reserved.
//

#import "BCMyCollectionVC.h"
#import "BCSiderCollectionViewCell.h"
#import "BCMineCollectionTableViewCell.h"
#import "BCFlagsTool.h"
#import "BCStuffFootView.h"

//临时数据
#define RowInSection 14

@interface BCMyCollectionVC () <UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) UICollectionView *siderCollectionView;
@property (nonatomic, strong) BCStuffFootView *footView;
@property (nonatomic, assign) int flag_value;
@end

@implementation BCMyCollectionVC

-(void)viewDidLoad {
    [super viewDidLoad];
    [self p_initNavgationBar];
    [self p_initMainView];
    [self p_initDiscriptionLable];
    [self p_initFootView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
//    NSNumber *num_flag = [[NSUserDefaults standardUserDefaults] valueForKey:@"k_flag_value"];
//    _flag_value = num_flag.intValue;
    
    //隐藏导航栏
    self.navigationController.navigationBar.hidden = NO;
    
    if (!self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    }
    
    //将来写在取得数据之后
    for (int i=0; i<RowInSection; i++) {
        int flag = read_flag(i, _flag_value);
        if (flag) {
            self.footView.amount +=1;
        }
    }
}

-(void)viewWillDisappear:(BOOL)animated {
    if (self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    }
}

#pragma mark - View init
-(void)p_initNavgationBar {
    self.title = @"我的收藏";
}


-(void)p_initDiscriptionLable {
    UILabel *discriptionLable = [[UILabel alloc] initWithFrame:CGRectMake(self.siderCollectionView.frame.size.width, 0, self.mainTableView.frame.size.width, 28.f)];
    discriptionLable.backgroundColor = [UIColor colorWithRed:0.902 green:0.902 blue:0.902 alpha:1.000];
    discriptionLable.textColor = [UIColor colorWithRed:0.114 green:0.506 blue:0.984 alpha:1.000];
    discriptionLable.text = @"  标价：为邦采推荐价格";
    discriptionLable.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:discriptionLable];
}

-(void)p_initMainView {
    self.mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width*0.2f, 28.f, self.view.frame.size.width-(self.view.frame.size.width*0.2), self.view.frame.size.height-self.navigationController.navigationBar.frame.size.height-20-28.f) style:UITableViewStylePlain];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, self.navigationController.navigationBar.frame.size.height+20.f, 0);
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.mainTableView.backgroundColor = [UIColor whiteColor];//[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    [self.view addSubview:self.mainTableView];
    ////////////////////////////////////////////////////
    
    UICollectionViewFlowLayout *sider_flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.siderCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.mainTableView.frame.origin.x, self.view.frame.size.height-self.navigationController.navigationBar.frame.size.height-20) collectionViewLayout:sider_flowLayout];
    self.siderCollectionView.delegate = self;
    self.siderCollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.siderCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    if (iPhone4_4S) {
        self.siderCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 65, 0);
    }
    [self.siderCollectionView registerClass:[BCSiderCollectionViewCell class] forCellWithReuseIdentifier:@"SecondSiderCell"];
    self.siderCollectionView.backgroundColor = [UIColor colorWithRed:0.941 green:0.937 blue:0.941 alpha:1.000];
    [self.view addSubview:self.siderCollectionView];
}

-(void)p_initFootView {
    self.footView = [[BCStuffFootView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-self.navigationController.navigationBar.frame.size.height-20-50, SCREEN_WIDTH, 50)];
//    self.footView.hidden = YES;
    [self.footView.coomit setTitle:@"重新上架" forState:UIControlStateNormal];
    self.footView.logoImg = [UIImage imageNamed:@"购物车1"];
    [self.view addSubview:self.footView];
    [self.footView.coomit addTarget:self action:@selector(commit:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - Pravit Action
- (void)commit:(UIButton *)but
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return RowInSection;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"mineConllectionCellIdentifer";
    BCMineCollectionTableViewCell *cell = [BCMineCollectionTableViewCell cellWithTableView:tableView];
    cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row < 10) {
        cell.selectedImageView.hidden = NO;
        cell.saleLable.hidden = YES;
        [cell.typeButton setBackgroundColor:[UIColor colorWithRed:0.016 green:0.451 blue:0.984 alpha:1.000]];
        
        int flag = read_flag((int)indexPath.row, _flag_value);
        if (flag) {
            cell.selectedImageView.image = [UIImage imageNamed:@"选中"];
        }
        else {
            cell.selectedImageView.image = [UIImage imageNamed:@"未选择"];
        }
    }
    else {
        cell.selectedImageView.hidden = YES;
        cell.saleLable.hidden = NO;
        [cell.typeButton setBackgroundColor:[UIColor colorWithRed:0.016 green:0.451 blue:0.984 alpha:1.000]];
        cell.selectedImageView.image = [UIImage imageNamed:@"未选择"];
    }
    
    __weak __typeof(&*cell)weakCell =cell;
    cell.selectedBlock = ^() {
        if ([weakCell.selectedImageView.image isEqual:[UIImage imageNamed:@"选中"]]) {
            weakCell.selectedImageView.image = [UIImage imageNamed:@"未选择"];
            _flag_value = write_flag((int)indexPath.row, _flag_value, FlagBool_NO);
            self.footView.amount -=1;
        }
        else {
            weakCell.selectedImageView.image = [UIImage imageNamed:@"选中"];
            _flag_value = write_flag((int)indexPath.row, _flag_value, FlagBool_YES);
            self.footView.amount +=1;
        }
    };
    
    return cell;
}

- (void)reloadData
{
    [self.mainTableView reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 118.f;
}

/*uitableview 滑动删除 begin*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        [self.infoItems removeObjectAtIndex:(indexPath.row*2)];
//        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView reloadData];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  @"取消收藏";
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
/*uitableview 滑动删除 end*/

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 8;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"SecondSiderCell";
    BCSiderCollectionViewCell *cell = [BCSiderCollectionViewCell cellWithCollectionView:collectionView];
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.title.text = @"禽蛋肉品";
        cell.backgroundColor = [UIColor whiteColor];
        cell.title.textColor = [UIColor colorWithRed:0.114 green:0.506 blue:0.984 alpha:1.000];
    }
    else if (indexPath.row == 1) {
        cell.title.text = @"蔬菜水果";
    }
    else if (indexPath.row == 2) {
        cell.title.text = @"水产冻品";
    }
    else if (indexPath.row == 3) {
        cell.title.text = @"熟食面点";
    }
    else if (indexPath.row == 4) {
        cell.title.text = @"酒水饮料";
    }
    else if (indexPath.row == 5) {
        cell.title.text = @"调料熟食";
    }
    else if (indexPath.row == 6) {
        cell.title.text = @"米面粮油";
    }
    else {
        cell.title.text = @"厨房用品";
    }
    
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    
    UIEdgeInsets top = {0,0,0,0};
    return top;
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.siderCollectionView.frame.size.width, 62.f);
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 25;
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0.f;
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *cells = [collectionView visibleCells];
    [cells enumerateObjectsUsingBlock:^(BCSiderCollectionViewCell*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        BCSiderCollectionViewCell *cell = (BCSiderCollectionViewCell *)obj;
        cell.backgroundColor = [UIColor clearColor];
        cell.title.textColor = [UIColor darkGrayColor];
    }];
    BCSiderCollectionViewCell *cell = (BCSiderCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.title.textColor = [UIColor colorWithRed:0.114 green:0.506 blue:0.984 alpha:1.000];
}

@end